package com.coderman.modelfly.service.impl;


import com.coderman.modelfly.Constants;
import com.coderman.modelfly.config.AnnotationConfig;
import com.coderman.modelfly.config.BeanConfig;
import com.coderman.modelfly.service.DbQueryService;
import com.coderman.modelfly.service.GenerateBean;
import com.coderman.modelfly.utils.DateBuilder;
import com.coderman.modelfly.utils.Dom4JUtils;
import com.coderman.modelfly.utils.GenerateTools;
import com.coderman.modelfly.utils.JdbcUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${fanchunshuai} on 2017-7-11.
 *
 * @author 58
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class GenerateDtoImpl implements GenerateBean {


    /**
     * 类上的注解描述
     */
    private AnnotationConfig classAnnotation;
    /**
     * id注解描述
     */
    private AnnotationConfig idAnnotation;
    /**
     * 普通注解描述
     */
    private AnnotationConfig fieldAnnotation;

    /**
     * 描述bean的公共类
     */
    private BeanConfig clazzBean;
    private DbQueryService dbQueryService = null;

    /**
     * 构造方法
     * @param configPath  插件配置路径
     * @param projectName 项目名称
     */
    public GenerateDtoImpl(String configPath,String projectName){
        BeanConfig beanConfig = null;
        try {
            beanConfig = Dom4JUtils.readBeanConfig(Constants.BEAN_DTO,configPath+"\\"+projectName);
            classAnnotation = beanConfig.getClazzAnnotationConfig();
            fieldAnnotation = beanConfig.getFieldAnnotationConfig();
            idAnnotation = beanConfig.getIdAnnotationConfig();

            this.setClazzBean(beanConfig);

            clazzBean.setClazzPath(configPath+"\\"+projectName);
            /**
             * 初始化db连接
             */
            dbQueryService = new DbQueryService(configPath+"\\"+projectName);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }





    @Override
    public boolean generateBeanFromTable() throws Exception {
        Map<String,List<String>> map = dbQueryService.getColumnMap();

        Map<String,String> tableDescMap = dbQueryService.getDBMap();

        try {
            map.forEach((k,v) ->{

                String className = GenerateTools.genearateBeanName(k,clazzBean.getPreffix(),clazzBean.getSuffix());

                Map<String,String>  map1 = GenerateTools.generateVariableList(null,fieldAnnotation,className,v);
                String head = generateHead(className,k,tableDescMap.get(k));
                String  varlist = map1.get("varList");
                String getSetList = map1.get("getSetList");
                String toString = map1.get("toString");

                try {
                    GenerateTools.writeFile(clazzBean.getClazzPath()+"\\dto",className,head,varlist,getSetList,toString);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }



    /**
     * 生成类头部信息
     * @param clazzName
     * @param tableName
     * @return
     */
    private String generateHead(String clazzName,String tableName,String tableDesc){
        StringBuffer buffer = new StringBuffer();
        //生成类头部
        String headStr = GenerateTools.generateClassHead(clazzBean,tableDesc);

        buffer.append(headStr);

        //生成类注释
        String classAnnotationStr = GenerateTools.generateClassAnnotation(classAnnotation,tableName);
        buffer.append(classAnnotationStr);

        //生成类描述
        String classDesc = GenerateTools.generateClassDesc(clazzBean,clazzName);
        buffer.append(classDesc);

        return buffer.toString();

    }




    public AnnotationConfig getClassAnnotation() {
        return classAnnotation;
    }

    public void setClassAnnotation(AnnotationConfig classAnnotation) {
        this.classAnnotation = classAnnotation;
    }

    public AnnotationConfig getIdAnnotation() {
        return idAnnotation;
    }

    public void setIdAnnotation(AnnotationConfig idAnnotation) {
        this.idAnnotation = idAnnotation;
    }

    public AnnotationConfig getFieldAnnotation() {
        return fieldAnnotation;
    }

    public void setFieldAnnotation(AnnotationConfig fieldAnnotation) {
        this.fieldAnnotation = fieldAnnotation;
    }

    public BeanConfig getClazzBean() {
        return clazzBean;
    }

    public void setClazzBean(BeanConfig clazzBean) {
        this.clazzBean = clazzBean;
    }
}
